package com.ms.teapuesto.util;

import com.ms.teapuesto.LogServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtilImpl {

    protected static final Logger LOG = LoggerFactory.getLogger(ZipUtilImpl.class);



    public byte[] comprimirArchivosAZip(Map<String, byte[]> files) throws Exception {
        ByteArrayInputStream isDocumentoOrigen = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
        try {
            Iterator it = files.entrySet().iterator();
            while (it.hasNext()) {
                byte[] buf = new byte[2156];
                Map.Entry file = (Map.Entry) it.next();
                isDocumentoOrigen = new ByteArrayInputStream((byte[]) file.getValue());
                zipOutputStream.putNextEntry(new ZipEntry((String) file.getKey()));
                int len;
                while ((len = isDocumentoOrigen.read(buf)) != -1) {
                    zipOutputStream.write(buf, 0, len);
                }
            }
            zipOutputStream.closeEntry();
        } catch (Exception excepcion) {
            LogServer.log.error("ERROR: Se produjo un error al comprimir archivo", excepcion);
            throw new Exception(excepcion);
        } finally {
            zipOutputStream.close();
            byteArrayOutputStream.close();
            isDocumentoOrigen.close();
        }
        return byteArrayOutputStream.toByteArray();
    }


    public byte[] comprimirArchivoAZip(byte[] entrada, String nombreArchivo) throws Exception {
        ByteArrayInputStream isDocumentoOrigen = new ByteArrayInputStream(entrada);
        byte[] salida = null;
        int iTamBuffer = 10240;
        byte[] buf = new byte[iTamBuffer];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipOutputStream zipOutputStream = null;
        try {
            zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
            zipOutputStream.putNextEntry(new ZipEntry(nombreArchivo));
            int len;
            while ((len = isDocumentoOrigen.read(buf)) > 0) {
                zipOutputStream.write(buf, 0, len);
            }
            zipOutputStream.closeEntry();
        } catch (Exception excepcion) {
            throw new Exception(excepcion);
        } finally {
            zipOutputStream.close();
            byteArrayOutputStream.close();
            isDocumentoOrigen.close();
        }
        salida = byteArrayOutputStream.toByteArray();
        return salida;
    }



    public byte[] descomprimirArchivo(byte[] entrada) throws Exception {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ZipInputStream zipEntradaStream = null;
        byte[] bytesArraySalida = null;
        int count = 0;

        zipEntradaStream = new ZipInputStream(new ByteArrayInputStream(entrada));
        byte data[] = new byte[10240];

        ZipEntry entry = null;
        entry = zipEntradaStream.getNextEntry();

        while ((count = zipEntradaStream.read(data, 0, 10240)) != -1) {

            byteArrayOutputStream.write(data, 0, count);

        }
        bytesArraySalida = new byte[byteArrayOutputStream.toByteArray().length];
        bytesArraySalida = byteArrayOutputStream.toByteArray();

        zipEntradaStream.close();
        byteArrayOutputStream.close();

        return bytesArraySalida;

    }

}
